/**
 *  IntegerItem.java : IceBird project
 *  23:23:18 
 */
package icebird.compiler.ncomp.vs;

import icebird.compiler.ncomp.X86Assembler;
import icebird.compiler.ncomp.X86Register;
import icebird.compiler.ncomp.X86Register.GPR;
import icebird.metadata.BasicType;

/**
 * Represents integer item in virtual stack.
 * @author Sergey Shulepoff[Knott]
 */
public final class IntegerItem extends WordItem {

	/**
	 * @param l Actual location.
	 */
	public IntegerItem(Location l) {
		super(l);
	}

	/* (non-Javadoc)
	 * @see icebird.compiler.ncomp.vs.WordItem#cloneConstant()
	 */
	protected WordItem cloneConstant() {
		if (!getLocation().isConstant())
			throw new IllegalStateException();
		
		IntegerConstantLocation c = (IntegerConstantLocation)getLocation();
		return new IntegerItem(IntegerConstantLocation.createIConst(c.getValue()));
	}

	/* (non-Javadoc)
	 * @see icebird.compiler.ncomp.vs.WordItem#loadConstant(icebird.compiler.ncomp.X86Register)
	 */
	protected void loadConstant(X86Register r) {
		if (!getLocation().isConstant())
			throw new IllegalStateException();
		
		X86Assembler asm = X86Assembler.getAsm();
		
		IntegerConstantLocation c = (IntegerConstantLocation)getLocation();
		if (c.getValue() == 0) {
			asm.clear(r);
		} else {
			asm.move(r, c.getValue());
		}
	}

	/* (non-Javadoc)
	 * @see icebird.compiler.ncomp.vs.WordItem#popFPU(icebird.compiler.ncomp.X86Register.GPR, int)
	 */
	protected void popFPU(GPR reg, int disp) {
		X86Assembler asm = X86Assembler.getAsm();
		asm.fistp(reg, disp);
	}

	/* (non-Javadoc)
	 * @see icebird.compiler.ncomp.vs.WordItem#pushConstant()
	 */
	protected void pushConstant() {
		if (!getLocation().isConstant())
			throw new IllegalStateException();
		
		X86Assembler asm = X86Assembler.getAsm();
		IntegerConstantLocation c = (IntegerConstantLocation)getLocation();
		
		asm.push(c.getValue());
	}

	/* (non-Javadoc)
	 * @see icebird.compiler.ncomp.vs.WordItem#pushFPU(icebird.compiler.ncomp.X86Register.GPR, int)
	 */
	protected void pushFPU(GPR reg, int disp) {
		X86Assembler asm = X86Assembler.getAsm();
		asm.fild(reg, disp);
	}

	/* (non-Javadoc)
	 * @see icebird.compiler.ncomp.vs.Item#getType()
	 */
	public BasicType getType() {
		return BasicType.Int;
	}
}
